package com.zkzx.company.base.service.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zkzx.company.base.config.AliSmsProperties;
import com.zkzx.company.base.service.sms.vo.SmsSend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SmsService {

    @Autowired
    private AliSmsProperties aliSmsProperties;

    public Boolean sendSms(String phoneNumbers,String templateCode,String templateParam){

        DefaultProfile profile = DefaultProfile.getProfile(aliSmsProperties.getRegionId(), aliSmsProperties.getAccessKeyId(), aliSmsProperties.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(aliSmsProperties.getSysDomain());
        request.setSysVersion(aliSmsProperties.getSysVersion());
        request.setSysAction(aliSmsProperties.getSysAction());
        request.putQueryParameter(SmsSend.PHONENUMBERS, phoneNumbers);
        request.putQueryParameter(SmsSend.SIGNNAME, aliSmsProperties.getSignName());
        request.putQueryParameter(SmsSend.TEMPLATECODE, templateCode);
        request.putQueryParameter(SmsSend.TEMPLATEPARAM, templateParam);
        log.info("阿里云短信调用参数:{} {} {}",phoneNumbers,templateCode,templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("阿里云短信返回结果:{}",response.getData());
            return true;
        } catch (Exception e) {
            log.error("阿里云短信异常信息:",e);
        }
        return false;
    }

}
